package com.nkym.controller;/*
 * @ClassName UserController
 * @Desc TODO
 * @Author 19637
 * @Date 2023/4/22 16:47
 * @Version 1.0
 */

import cn.hutool.http.HttpRequest;
import com.nkym.annotations.AuthCheck;
import com.nkym.common.BaseResponse;
import com.nkym.common.ErrorCode;
import com.nkym.constants.UserConstant;
import com.nkym.exception.BusinessException;
import com.nkym.pojo.dto.IdRequest;
import com.nkym.pojo.dto.user.SecretKeyRequest;
import com.nkym.pojo.dto.user.UserAddRequest;
import com.nkym.pojo.dto.user.UserLoginRequest;
import com.nkym.pojo.entity.User;
import com.nkym.service.UserService;
import com.nkym.utils.RSAUtil;
import com.nkym.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
@Api(tags = "userAdmin")
public class UserController {

    @Resource
    UserService userService;

    @GetMapping("/get")
    public BaseResponse<User> getUserById(IdRequest idRequest, HttpServletRequest request) {
        Integer id = idRequest.getId();
        if (id == null) {
            throw new BusinessException(ErrorCode.PARAM_ERROR);
        }
        User user = userService.getById(id);

        return ResultUtil.success(user);
    }

    @GetMapping("/current")
    public BaseResponse<User> currentUser(HttpServletRequest request) {
        User user = userService.currentUser(request);
        return ResultUtil.success(user, "获取成功");
    }

    @PostMapping("/getPublicKey")
    @ApiOperation("获取RSA秘钥")
    public BaseResponse<String> getPublicKey() {
        String publicKey = RSAUtil.getPublicKeyStr();
        return ResultUtil.dataOk(publicKey);
    }

    @PostMapping("/getSecretKey")
    @ApiOperation("获取用户secretKey")
    public BaseResponse<String> getSecretKey(@RequestBody String encryptData, HttpServletRequest request) {
        String res = userService.getSecretKey(encryptData, request);
        return ResultUtil.dataOk(res);
    }

    @PostMapping("/register")
    public BaseResponse<User> registerUser(@RequestBody UserAddRequest userAddRequest) {
        boolean b = userService.registerUser(userAddRequest);
        if (b) {
            return ResultUtil.success("注册成功");
        } else {
            return ResultUtil.fail("注册失败");
        }
    }

    @PostMapping("/login")
    public BaseResponse<User> userLogin(UserLoginRequest userLoginRequest, HttpServletRequest request, HttpServletResponse response) {
        User user = userService.userLogin(userLoginRequest, request, response);
        if (user != null) {
            return ResultUtil.success(user, "登录成功");
        } else {
            return ResultUtil.fail("登录成功");
        }
    }

    @GetMapping("/logout")
    public BaseResponse<User> userLogout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute(UserConstant.USER_LOGIN_STATUS);
        return ResultUtil.success();
    }


}
